﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERP.Page;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class JobUC : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertJob();
            }
            else
            {
                UpdateJob();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {

        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsJob = new Job_BLL();
            var dtJob = clsJob.BindGirdtb_Job(string.Empty);
            if (dtJob != null && dtJob.Rows.Count > 0)
            {
                foreach (DataRow dr in dtJob.Rows)
                {
                    if (txtChucVu.Text == dr["Job_Name"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng chức vụ !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertJob()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Job_ET
                    {
                        Job_Name = txtChucVu.Text,
                        Job_Note = txtGhiChu.Text,
                        Job_Date_Add = DateTime.Now,
                        Job_Date_Modify = DateTime.Now
                    };
                    Job_BLL.Inserttb_Job(obj);
                    ClearTextBox();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtChucVu.Enabled = true;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateJob()
        {
            try
            {
                var obj = new Job_ET
                {
                    Job_ID = int.Parse(lbID.Value),
                    Job_Name = txtChucVu.Text,
                    Job_Note = txtGhiChu.Text,
                    Job_Date_Add = DateTime.Now,
                    Job_Date_Modify = DateTime.Now
                };
                Job_BLL.Updatetb_Job(obj);
                ((Job)Page).GetJob();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtChucVu.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtChucVu.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
        }
        #endregion
    }
}